package com.linkage.educloud.ah.view.activity.app;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.EduResAdapter;
import com.linkage.educloud.ah.bean.EduResBean;
import com.linkage.educloud.ah.mvp.contract.IEduResListContract;
import com.linkage.educloud.ah.mvp.presenter.EduResListPresenter;
import com.linkage.educloud.ah.view.BasicTitleActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/12/20.
 */

public class EduResListActivity extends BasicTitleActivity implements IEduResListContract.IView {

    @BindView(R.id.common_recylerview)
    XRecyclerView common_recylerview;

    private static final String KEY_TYPE = "type";
    private static final String KEY_SKEY = "skey";

    IEduResListContract.IPresenter iPresenter;
    EduResAdapter adapter;
    int page = 1;
    List<EduResBean> list = new ArrayList<>();

    public static void launch(Context context, int type, String skey) {
        Intent intent = new Intent(context, EduResListActivity.class);
        intent.putExtra(KEY_TYPE, type);
        intent.putExtra(KEY_SKEY, skey);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.common_one_list;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("全部资源");
        iPresenter = new EduResListPresenter(this, this);
        initRecylerview();
        showZloading();
        iPresenter.searchResouces(getIntent().getStringExtra(KEY_SKEY), getIntent().getIntExtra(KEY_TYPE, 0), page);
    }

    private void initRecylerview() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        common_recylerview.setLayoutManager(layoutManager);
        common_recylerview.setItemAnimator(new DefaultItemAnimator());
        common_recylerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        common_recylerview.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);

        common_recylerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                iPresenter.searchResouces(getIntent().getStringExtra(KEY_SKEY), getIntent().getIntExtra(KEY_TYPE, 0), page);
            }

            @Override
            public void onLoadMore() {
                page++;
                iPresenter.searchResouces(getIntent().getStringExtra(KEY_SKEY), getIntent().getIntExtra(KEY_TYPE, 0), page);
            }
        });

        adapter = new EduResAdapter(this, list);
        common_recylerview.setAdapter(adapter);
    }

    public void initDate(List<EduResBean> mList) {
        hideZloading();
        if (mList.size() < 10) {
            common_recylerview.setLoadingMoreEnabled(false);
        } else {
            common_recylerview.setLoadingMoreEnabled(true);
        }
        adapter.addAllForSearch(mList, page != 1);
    }

    public void stopRefresh() {
        common_recylerview.refreshComplete();
        common_recylerview.loadMoreComplete();
    }
}
